package com.roan.alipay.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.roan.alipay.entity.Product;
import com.roan.alipay.service.AlipayService;
import com.roan.alipay.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Roan
 * @date 2025/4/26 0:17
 */
@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private AlipayClient alipayClient;

    @Autowired
    private ProductService productService;

    @Override
    public AlipayTradePagePayResponse pay(String productId) {
        // TODO: 查询商品信息
        Product product = productService.getById(productId);

        // 请求 alipay API
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo("20150320010101001");
        model.setTotalAmount(product.getPrice());
        model.setSubject(product.getTitle());
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        AlipayTradePagePayResponse response;
        try {
            response = alipayClient.pageExecute(request, "POST");
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }

        if (response.isSuccess()) {
            log.info("receive pay response:{}", response);
        }else {
            log.error("receive failure: {}", response.getMsg());
        }
        return response;
    }

    @Override
    public AlipayTradeCloseResponse query(String transactionId) {
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        AlipayTradeCloseModel model = new AlipayTradeCloseModel();

        model.setTradeNo("2013112611001004680073956707");

        // 设置订单支付时传入的商户订单号
        model.setOutTradeNo("HZ0120131127001");

        // 设置商家操作员编号 id
        model.setOperatorId("YX01");

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeCloseResponse response;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        log.info("{}", response.getBody());

        if (response.isSuccess()) {
            log.info("query success: {}", response);
        } else {
            log.error("query failed: {}", response.getMsg());
        }
        return response;
    }
}
